package com.thd.springboottest.sso.server.vo;


import com.thd.springboottest.sso.vo.AuthBean;

import java.util.*;

/**
 * com.thd.springboottest.sso.server.vo.SessionUtil
 *
 * @author: wanglei62
 * @DATE: 2021/8/20 18:31
 **/
public class SessionUtil {
    public static Map<String, AuthBean> authBeanList = new HashMap<String,AuthBean>();

    public static void push(String ssoToken,AuthBean authBean){
        SessionUtil.authBeanList.put(ssoToken,authBean);
    };

    public static void remove(String ssoToken){
        SessionUtil.authBeanList.remove(ssoToken);
    };

    public static AuthBean get(String ssoToken){
        return SessionUtil.authBeanList.get(ssoToken);
    };

    public static boolean check(String ssoToken){
        AuthBean authBean =  SessionUtil.authBeanList.get(ssoToken);
        return authBean != null;
    };

    public static AuthBean getByUsr(String usr){
        Collection<AuthBean> list = authBeanList.values();
        Optional<AuthBean> optional = list.stream().filter(item -> {
            return item.getUsr().equalsIgnoreCase(usr);
        }).findFirst();
        if(optional.isPresent()){
            return optional.get();
        }else{
            return null;
        }
    }

}
